///
/// @DIR_PATH：lib/study
/// @TIME：2023/11/10 11:18
/// @AUTHOR：starr

class Factory{
  late final String name;
  //用来缓存对象
  static final Map<String,Factory> _cache=<String,Factory>{};
  factory Factory(String name){
    return _cache.putIfAbsent(name, () => Factory._internal(name));
  }

  //私有化构造函数
  Factory._internal(this.name){
    print("生成新的实例：$name");
  }
}

class Singleton{
  static final Singleton _singleton=Singleton._internal();
  factory Singleton(){
    return _singleton;
  }
  Singleton._internal();
}

main(){
  var p1=Factory("1");
  var p2=Factory("2");
  var p3=Factory("1");
  //identical会对两个对象的地址进行比较，相同返回true，
  print(identical(p1, p2));
  //返回ture，说明p1和p2就是同一个对象
  print(identical(p1, p3));
}